package com.jd.mysql.mgr.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jd.mysql.mgr.dao.MgrAccountDAO;
import com.jd.mysql.mgr.pojo.MgrAccount;

public class MgrAccountDAOImpl extends HibernateDaoSupport implements MgrAccountDAO {

	public void deleteMgrAccount(MgrAccount mgra) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(mgra);
	}

	public void saveMgrAccount(MgrAccount mgra) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(mgra);
	}

	public void updateMgrAccount(MgrAccount mgra) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.update(mgra);
	}

	public MgrAccount findMgrAccountById(Integer id) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().get(MgrAccount.class, id);
	}


	public MgrAccount findMgrAccountByUsernameAndPassword(String username,
			String password) {
		// TODO Auto-generated method stub
		String hql="from MgrAccount as mgra where mgra.username=:username and mgra.password=:password and yn=true";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setString("username", username);
		query.setString("password", password);
		List<MgrAccount> list = query.list();
		if(list!=null && list.size()==1){
			return list.get(0);
		}else{
			return null;	
		}
	}

	public List<MgrAccount> getAccountList() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MgrAccount> getAccountList(Integer start, Integer limit,Integer groupId) {
		// TODO Auto-generated method stub
		String hql="from MgrAccount where yn=true ";
		if(groupId!=-1){
			hql+="and mgrAccountGroup.id="+groupId+" ";
		}
		hql+="order by id desc";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List<MgrAccount> list = query.list();
		return list;
	}

	public MgrAccount findMgrAccountByUserName(String username) {
		// TODO Auto-generated method stub
		String hql="from MgrAccount where username=:username";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setString("username", username);
		List<MgrAccount> list = query.list();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public Long getAccountTotalCount(Integer groupId,Boolean enable) {
		// TODO Auto-generated method stub
		Long count = 0l;
		String hql="select count(*) as count from MgrAccount where 1=1 ";
		if(enable){
			hql +="and yn=true ";
		}
		if(groupId!=-1){
			hql +="and mgrAccountGroup.id="+groupId;
		}
		Session session = getSession();
		Query query = session.createQuery(hql);
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			count = list.get(0);
		}
		return count;
	}
}
